package engine;

public class Matrix2X2 {
	
	private float[][] data = new float[2][2];



	private Vector translation;
	/**
	 * Matrix2X2 Constructor
	 * @param a11 1st row 1st column element
	 * @param a12 1st row 2nd column element
	 * @param a21 2nd row 1st column element
	 * @param a22 2nd row 2nd column element
	 */
	public Matrix2X2(float a11, float a12, float a21, float a22){
		// first row
		data[0][0] = a11;
		data[1][0] = a12;
		// second row
		data[0][1] = a21;
		data[1][1] = a22;
	}
	
	/**
	 * Create a rotation matrix based off of a supplied angle.
	 * @param angle
	 */
	public Matrix2X2(float angle){
		// first row
		data[0][0] = (float)Math.cos(angle);
		data[1][0] = (float)-Math.sin(angle);
		// second row
		data[0][1] = (float)Math.sin(angle);
		data[1][1] = (float)Math.cos(angle);
	}
	/**
	 * Computes the determinate of the current Matrix2X2 object
	 * @return the determinate of the matrix
	 */
	/*
	 * @returns float;
	 * @assignable \nothing;
	 * @requires this!=null;
	 */
	public float det(){
		return data[0][0] * data[1][1] - data[0][1] * data[1][0];
	}
	/**
	 * applyTransform applies a transformation to the 2x2 vector
	 * @param v the vector to use in the transformation
	 * @return the transformed vector
	 */
	/*
	 * @requires this!=null;
	 * @requires v!=null;
	 * @returns Vector;
	 */
	public Vector applyTransform(Vector v){
		if(v==null){System.out.println("Illegal Input, cannot have null transformation vector");return null;}
		
		return new Vector(data[0][0] * v.getX() + data[1][0] * v.getY() + translation.getX(),
						  data[0][1] * v.getX() + data[1][1] * v.getY() + translation.getY());
	}
	/**
	 * setTranslation sets the translation vector of the 2x2 matrix
	 * @param translation the new translation vector
	 */
	/*
	 * @requires this!=null;
	 * @requires translation!=null;
	 * @returns \nothing;
	 */
	public void setTranslation(Vector translation) {
		if(translation==null){System.out.println("Illegal Input, cannot have null translation vector");return;}
		this.translation = translation;
	}
	/**
	 * getTranslation gets the translation Vector of the 2x2 matrix
	 * @return the translation vector
	 */
	/*
	 * @requires this!=null;
	 * @return Vector;
	 */
	public Vector getTranslation() {
		return translation;
	}
	/**
	 * Computes the determinate of a 2x2 matrix
	 * @param a11 first entry in the matrix, row 1 column 1
	 * @param a12 second entry in the matrix, row 1 column 2
	 * @param a21 third entry in the matrix, row 2 column 1
	 * @param a22 fourth entry in the matrix, row2 column 2
	 * @return the determinate of the specified matrix
	 */
	/*
	 * @returns float;
	 * @assignable \nothing
	 */
	public static float det(float a11, float a12, float a21, float a22 ){
		return a11 * a22 - a21 * a12;
	}
}
